﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.Net.Mail;
using mesoBoard.Data.Repositories;
using System.Collections.Specialized;
using mesoBoard.Data;
using System.Web.Mvc;

namespace mesoBoard.Services
{
    public static class Emailing
    {
        private static void SendEmail(
            string toEmail, 
            string fromEmail, 
            string subject, 
            string templateRelativePath, 
            ListDictionary replacements, 
            bool isHtml = true)
        {
            // Add standard replacements
            replacements.Add("{BOARD_URL}", SiteConfig.BoardURL.Value);
            replacements.Add("{BOARD_NAME}", SiteConfig.BoardName.Value);

            MailDefinition md = new MailDefinition();

            // specify the location of template and email details
            md.BodyFileName = templateRelativePath;
            md.IsBodyHtml = isHtml;
            md.From = fromEmail;
            md.Subject = subject;

            MailMessage message = md.CreateMailMessage(toEmail, replacements, new System.Web.UI.Control());

            //Send the message
            SmtpClient client = new SmtpClient();

            try
            {
                client.Send(message);
            }
            catch
            {

            }
        }

        public static void WelcomeEmail(User TheUser)
        {
            ListDictionary replacements = new ListDictionary();


            replacements.Add("{USERNAME}", TheUser.Username);

            SendEmail(
                TheUser.Email,
                SiteConfig.AutomatedFromEmail.Value,
                "Welcome - " + SiteConfig.BoardName,
                "~/Templates/Email/Welcome.txt",
                replacements);
        }

        public static void Registration(User TheUser, string confirm_url)
        {
            ListDictionary replacements = new ListDictionary();


            replacements.Add("{USERNAME}", TheUser.Username);
            replacements.Add("{CONFIRM_URL}", confirm_url);

            SendEmail(
                TheUser.Email,
                SiteConfig.AutomatedFromEmail.Value,
                "Account Activation Required - " + SiteConfig.BoardName.Value,
                "~/Templates/Email/Registration.txt",
                replacements);
        }

        public static void ResendActivationCode(User TheUser, string confirm_url)
        {
            ListDictionary replacements = new ListDictionary();


            replacements.Add("{USERNAME}", TheUser.Username);
            replacements.Add("{CONFIRM_URL}", confirm_url);

            SendEmail(
                TheUser.Email,
                SiteConfig.AutomatedFromEmail.Value,
                "Account Activation Required - " + SiteConfig.BoardName.Value,
                "~/Templates/Email/ResendActivationCode.txt",
                replacements);
        }

        public static void NewPostEmail(IEnumerable<Subscription> Subs, Post ThePost, Thread TheThread, string PostURL)
        {
            ConfigRepository ConfigRep = new ConfigRepository(new mbEntities());
            UserRepository UserRep = new UserRepository(new mbEntities());
            ListDictionary replacements = new ListDictionary();
            string AutoFromEmail = ConfigRep.Value("AutomatedFromEmail");

            //Create MailDefinition
            MailDefinition md = new MailDefinition();

            md.BodyFileName = "~/Templates/Email/NewThreadReply.txt";
            md.IsBodyHtml = true;
            md.From = AutoFromEmail;
            md.Subject = "Reply to Thread '" + TheThread.FirstPost.Title + "' - " + SiteConfig.BoardName.Value;

            replacements.Add("{REPLY_USERNAME}", ThePost.User.Username);
            replacements.Add("{REPLY_TEXT}", ThePost.Text);
            replacements.Add("{THREAD_TITLE}", TheThread.FirstPost.Title);
            replacements.Add("{POST_URL}", PostURL);
            replacements.Add("{BOARD_URL}", SiteConfig.BoardURL.Value);
            replacements.Add("{BOARD_NAME}", SiteConfig.BoardName.Value);
             

            System.Web.UI.Control ctrl = new System.Web.UI.Control { ID = "Something" };

            MailMessage message = md.CreateMailMessage("subemails@mesocube.com", replacements, ctrl);

            //Send the message
            SmtpClient client = new SmtpClient();

            foreach (Subscription s in Subs)
            {
                if (s.UserID == ThePost.UserID) continue;
                User u = UserRep.GetUser(s.UserID);
                message.To.Clear();
                message.To.Add(u.Email);
                try
                {
                    client.Send(message);
                }
                catch
                {
                }
            }
        }

        public static void PasswordChanged(User TheUser, string NewPassword)
        {
            ListDictionary replacements = new ListDictionary();

            replacements.Add("{USERNAME}", TheUser.Username);
            replacements.Add("{USERPASSWORD}", NewPassword);

            SendEmail(
                TheUser.Email,
                SiteConfig.AutomatedFromEmail.Value,
                "Password Changed - " + SiteConfig.BoardName,
                "~/Templates/Email/PasswordChange.txt",
                replacements);
        }

        public static void EmailChanged(User TheUser)
        {
            ListDictionary replacements = new ListDictionary();


            replacements.Add("{USERNAME}", TheUser.Username);
            replacements.Add("{EMAIL}", TheUser.Email);

            SendEmail(
                TheUser.Email,
                SiteConfig.AutomatedFromEmail.Value,
                "Account Activation Required - " + SiteConfig.BoardName.Value,
                "~/Templates/Email/EmailChange.txt",
                replacements);
        }

        public static void PasswordResetRequest(User TheUser, string ResetURL)
        {
            ListDictionary replacements = new ListDictionary();

            replacements.Add("{USERNAME}", TheUser.Username);
            replacements.Add("{RESET_URL}", SiteConfig.BoardURL.Value + ResetURL);


            SendEmail(
                TheUser.Email,
                SiteConfig.AutomatedFromEmail.Value,
                "Password Reset Request - " + SiteConfig.BoardName.Value,
                "~/Templates/Email/PasswordResetRequest.txt",
                replacements);
        }

        public static void ContactUs(string Name, string Email, string Subject, string Message)
        {
            ListDictionary replacements = new ListDictionary();


            replacements.Add("{NAME}", Name);
            replacements.Add("{EMAIL}", Email);
            replacements.Add("{SUBJECT}", Subject);
            replacements.Add("{MESSAGE}", Message);

            SendEmail(
                SiteConfig.ContactUsToEmail.Value,
                Email,
                "Contact Us Form - " + SiteConfig.BoardName.Value,
                "~/Templates/Email/ContactUs.txt",
                replacements);
        }

        public static void NewMessage(Message TheMessage, User ToUser, string messageURL)
        {
            ListDictionary replacements = new ListDictionary();


            replacements.Add("{FROMUSERNAME}", TheMessage.UserFrom.Username);
            replacements.Add("{MESSAGEURL}", messageURL);

            SendEmail(
                ToUser.Email,
                SiteConfig.AutomatedFromEmail.Value,
                "New Message - " + SiteConfig.BoardName.Value,
                "~/Templates/Email/NewMessage.txt",
                replacements);
        }
    }
}
